package pl.umk.mat.fred.unicore.urc.storagemanagement.tree;

import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

import pl.umk.mat.fred.unicore.urc.storagemanagement.StorageManagementActivator;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.CatalogNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.FileNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.GridNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.MultiplyCatalogNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.StorageNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.utils.TreeUtils;

public class LabelProviderImplementation implements ILabelProvider {

	@Override
	public void addListener(ILabelProviderListener arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean isLabelProperty(Object arg0, String arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void removeListener(ILabelProviderListener arg0) {
		// TODO Auto-generated method stub	
	}

	@Override
	public Image getImage(Object arg0) {
		if (arg0 instanceof StorageNode) {
			ImageDescriptor image = (ImageDescriptor)StorageManagementActivator.getDefault().getObjectFromCache(StorageNode.ICON_NAME);
			if (image == null) {
			  image = ImageDescriptor.createFromURL(StorageManagementActivator.getDefault().getBundle().getEntry("/icons/database.png"));
			  StorageManagementActivator.getDefault().setObjectToCache(StorageNode.ICON_NAME, image);
			}
			if (image != null) {
				return image.createImage();
			}
			return null;
		}
		if (arg0 instanceof CatalogNode) {
			CatalogNode cn = (CatalogNode) arg0;
			return getCatalogIcon(cn.isOpened());
		}
		if (arg0 instanceof MultiplyCatalogNode) {
			MultiplyCatalogNode mcn = (MultiplyCatalogNode) arg0;
			return getCatalogIcon(mcn.isOpened());
		}
		if (arg0 instanceof FileNode) {
			return PlatformUI.getWorkbench().getSharedImages().
					getImageDescriptor(ISharedImages.IMG_OBJ_FILE).createImage();
		}
		if (arg0 instanceof GridNode) {
			ImageDescriptor image = (ImageDescriptor)StorageManagementActivator.getDefault().getObjectFromCache(GridNode.ICON_NAME);
			if (image == null) {
			  image = ImageDescriptor.createFromURL(StorageManagementActivator.getDefault().getBundle().getEntry("/icons/grid.png"));
			  StorageManagementActivator.getDefault().setObjectToCache(GridNode.ICON_NAME, image);
			}
			if (image != null) {
				return image.createImage();
			}
			return null;
		}
		return null;
	}

	@Override
	public String getText(Object arg0) {
		if (arg0 instanceof StorageNode) {
			StorageNode node = (StorageNode) arg0;
			String result = node.getStorage().getName();
			Long space = node.getFreeSpace();
			if (space != null) {
				result += " ( free space: " + TreeUtils.formatStorageSpace(space) + " )";
			}
			return result;
		}
		if (arg0 instanceof CatalogNode) {
			CatalogNode node = (CatalogNode) arg0;
			return node.getName();
		}
		if (arg0 instanceof MultiplyCatalogNode) {
			MultiplyCatalogNode node = (MultiplyCatalogNode) arg0;
			return node.getName();
		}
		if (arg0 instanceof FileNode) {
			FileNode node = (FileNode) arg0;
			return node.getName();
		}
		if (arg0 instanceof GridNode) {
			return "Grid";
		}
		return "";
	}
	
	private Image getCatalogIcon(boolean opened) {
		if (opened) {
			return PlatformUI.getWorkbench().getSharedImages().
			getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER).createImage();
		}
		ImageDescriptor image = (ImageDescriptor)StorageManagementActivator.getDefault().getObjectFromCache(CatalogNode.CLOSED_ICON_NAME);
		if (image == null) {
			image = ImageDescriptor.createFromURL(StorageManagementActivator.getDefault().getBundle().getEntry("/icons/closed_folder.png"));
			StorageManagementActivator.getDefault().setObjectToCache(CatalogNode.CLOSED_ICON_NAME, image);
		}
		if (image != null) {
			return image.createImage();
		}
		return null;
	}

}
